package com.server;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.database.AdministratorDB;

public class ManagerLoginServlet extends CustomerServlet
{
	private static final long serialVersionUID = 1L;
	private final String className = "Manager Login Servlet: ";

	@Override
	//@requires HttpServletRequest !=NULL,  HttpServletResponse !=NULL ;
    //@assignable employeeID_string,password;
    //@ensures db.UserExists(employeeID_int)==true, password=db.GetPassword;
	//@signals_only RuntimeException, IOException,ServletException;
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		ConnectDB();
		AdministratorDB db = new AdministratorDB();
		db.Setup(connection);
				
		String employeeID_string = req.getParameter("id");
		String password = req.getParameter("password");
		
		if (NaN(employeeID_string))//if employee ID is invalid, user will be directed to the login failed page
		{
			req.getRequestDispatcher("customer/login_failed_customer.jsp").forward(req, resp);
		}
		else
		{
			int employeeID_int = Integer.parseInt(employeeID_string);
				
			try //if username and psw are correct, user will login successfully, otherwise will directed to a login failed page
			{
				if (db.UserExists(employeeID_int))
				{
					if (password.equals(db.GetPassword()))
					{					
						req.getRequestDispatcher("index_manager.jsp").forward(req, resp);
					}
					else
					{
						req.getRequestDispatcher("customer/login_failed_customer.jsp").forward(req, resp);
					}
				}
				else
				{
					req.getRequestDispatcher("customer/login_failed_customer.jsp").forward(req, resp);
				}
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
		}
	}

}

